class Solution
{
public:
    int matrixSum(vector<vector<int>> &nums)
    {
        for (auto &row : nums)
        {
            sort(row.begin(), row.end());
        }

        int result = 0;
        int m = nums.size();
        int n = nums[0].size();
        for (int i = 0; i < n; ++i)
        {
            int maxVal = 0;
            for (int j = 0; j < m; ++j)
            {
                maxVal = max(maxVal, nums[j][i]);
            }
            result += maxVal;
        }
        return result;
    }
};